// 审批流按钮事件脚本
import layerFun from '@/common/comFun/layerFun.js';
var layer = layerFun;

function ApprovalFun() {
	return {
		getGlobalData: function(vm) {
			vm.Request.requestByUrl('/FormDetail/GetDataByName', {
				objName: vm.formData.FormName
			}, function(data) {
				vm.globalData = data

				let approvalConfig = {
					isEnableApproval: false
				}
				if (data.ds[0].hasOwnProperty("approvalConfig")) {
					let theApprovalConfig = JSON.parse(data.ds[0].approvalConfig);
					if (theApprovalConfig.hasOwnProperty("isEnableApproval")) {
						approvalConfig.isEnableApproval = theApprovalConfig.isEnableApproval;
					}
					approvalConfig = Object.assign(approvalConfig, theApprovalConfig);
				}
				vm.approvalConfig = approvalConfig

				if (vm.approvalConfig.isEnableApproval) {
					let comApprovalConfig =
						"select * from Ba_Audit where isnull(History,0)=0 and SourceTable='" +
						vm.physicalTableName + "' and SourceID=" + vm.keyValue;

					vm.Request.requestByUrl("/SQLData/QuerySql", {
						sql: comApprovalConfig
					}, function(ret) {
						if (ret.ds.length > 0) {
							vm.isShowComApproval = true;

							vm.ApprovalID = ret.ds[0].APFlowID;

							// 是否可以审批
							vm.Request.request('WhetherItCanBeApproved', {
								P_TableName: vm.physicalTableName,
								P_PKValue: vm.keyValue
							}, function(data) {
								console.log(data)
								if (data.data.ds[0].ISSP == 1) {
									vm.isCurApproval = true;
								} else {
									vm.isCurApproval = false;
								}
								vm.ApprovalSort = data.data.ds[0].ApprovalSort;
							})

						}
					})
				}
			})
		},
		startApprove: function(vm) { // 发起审批
			//判定是否启用审批流
			if (vm.approvalConfig.isEnableApproval) {
				// 二开配置项数据
				let globalData = vm.globalData.ds[0];

				//判断审批流的类型
				var approvalSql = "select * from SYS_BillAPFlow a where isnull(Type,'')!='999' and FormID=" +
					globalData
					.Id + "  ";
				// //查询审批记录权限

				vm.Request.requestByUrl("/SQLData/QuerySql", {
					sql: approvalSql
				}, function(data) {
					const apType1Items = data.ds.filter(item => item.APType == 1);
					let filterData = apType1Items.length > 0 ? apType1Items : data.ds.filter(item => item
						.APType == 2);
					if (filterData.length) {
						ApprovalFun().configApprove(vm, filterData);
					} else {
						layer.msg('该单据暂未配置审批流方式');
					};
					/*
					if (data.ds.length) {
						ApprovalFun().configApprove(vm, data.ds);
					} else {
						layer.msg('该单据暂未配置审批流方式');
					};
					*/
				});
			} else {
				layer.msg('该单据暂未开启审批流');
			};
		},
		configApprove(vm, approveInfo) { // 根据APType启用不同的审批流

			/**
			 * approveInfo.APType 是否固定审批流  1--固定审批流  or 非固定式审批流
			 * globalData 二开配置项数据
			 */
			let globalData = vm.globalData.ds[0];
			if (approveInfo[0].APType == 1) { // 固定审批流
				vm.$popup.show({
					option: approveInfo, // 打开的选项弹层的数据源
					globalData: globalData,
					keyName: vm.keyName,
					keyValue: vm.keyValue,
					confirm: (data) => {

						let workConfig = vm.approvalConfig.workConfig;

						let work = workConfig[0];

						var script = work.script.replace(/\['/g, "vm.formData['");
						script = script.replace(/\["/g, "vm.formData[\"");
						eval(script);


					},
					cancel: () => {

					}
				})
			} else {
				let approveObj = vm.$utils.deepClone(vm.$store.state.st_comPageConfig.approveObj);

				approveObj['workFlowDef']['name'] = approveInfo[0].BillName;

				Vue.prototype.$store.commit('ChangeAndSetStateData', {
					Name1: 'BaseScreenParm',
					data: {
						option: approveInfo,
						globalData: globalData,
						keyName: vm.keyName,
						keyValue: vm.keyValue,
						approveObj: approveObj,
					}
				})

				uni.navigateTo({
					url: '/uni_modules/mc-ui/components/m-node-warp/compontentPage/m-globalPopup/m-nodeWarpPage?source=QMS',
					events: {
						confirm: function(data) {

						},
						doneConfirm: (data) => { // 触发按钮的加载完事件脚本

							let workConfig = vm.approvalConfig.workConfig

							let work = workConfig[data];
							console.log(work);

							var script = work.script.replace(/\['/g,
								"vm.formData['");
							script = script.replace(/\["/g, "vm.formData[\"");
							eval(script);
							// 刷新页面
							vm.refreshPage();


						},
						cancel: function(data) {

						}
					}
				})
			}


		},
		formApprove: function(vm) { // 审批单据
			// 二开配置项数据
			let globalData = vm.globalData.ds[0];
			vm.$popup.showFormApprove({
				globalData: globalData,
				keyName: vm.keyName,
				keyValue: vm.keyValue,
				ApprovalID: vm.ApprovalID,
				ApprovalSort: vm.ApprovalSort,
				Status: vm.formData.Status,
				confirm: (data) => { // 确认按钮
					// 刷新页面
					vm.refreshPage();

				},
				doneConfirm: (data) => { // 触发按钮的加载完事件脚本
					let workConfig = vm.approvalConfig.workConfig

					let work = workConfig[data];

					var script = work.script.replace(/\['/g, "vm.formData['");
					script = script.replace(/\["/g, "vm.formData[\"");
					eval(script);
					// 刷新页面
					vm.refreshPage();


				},
				cancel: (data) => {

				}
			})
		},
		cancelApprove: function(vm) { // 取消审批
			let param = {
				tableName: vm.physicalTableName,
				keyName: vm.keyName,
				keyId: vm.keyValue,
				userId: vm.$store.state.UserInfo.UserID,
			};

			vm.Request.requestByUrl("/Approve/cancelApproval", param, function(data) {

				if (data.ds[0]['Status'] == 200) {
					uni.showToast({
						title: '撤销审批成功'
					});
					// 刷新页面
					vm.refreshPage();

				} else if (data.ds[0]['Status'] == 500) {
					layer.msg(data.ds[0]['msg'])
				}
			})
		},
		viewListApprove: function(vm) { // 查阅审批流
			// 二开配置项数据
			let globalData = vm.globalData.ds[0];
			vm.$popup.showListApprove({
				tableName: vm.physicalTableName,
				globalData: globalData,
				keyName: vm.keyName,
				keyValue: vm.keyValue,
				ApprovalSort: vm.ApprovalSort,
				Status: vm.formData.Status,
				confirm: (data) => { // 确认按钮 

				},
				cancel: (data) => {

				}
			})
		}
	}
}

export default ApprovalFun();